package com.yunzo.coc.common.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
/**
 * 短信相关的工具类*
 * @author xiaobo
 */
public class SMSUtils {

	/**
	//促销信息帐号
		ID:3485
		账号：szyzkj
		密码：szyzkj456789
		
		//验证码帐号
		ID:3572
		账号：szyzkj2
		密码：szyzkj456789
	 * 
	 * 
	 */
	//发送短信
	private static final String SMSAPI="http://211.147.242.161:8888/sms.aspx";
	
	private static final String YUNZO="【云筑圈】";
	public static String sendSMS(String mobiles,String msgtest)
	{
		String responseCode = null;
		Map<String, String> params = new HashMap<String, String>();  
		params.put("action", "send");  
		params.put("userid", "3485");  
		params.put("account", "szyzkj");  
		params.put("password", "szyzkj456789");  
		params.put("mobile", mobiles);  
		params.put("content",YUNZO+msgtest);  
		params.put("sendTime", "");  
		params.put("extno", "");  
		responseCode = HttpClientUtil.post(SMSAPI,params);
		
		return responseCode;
	}
	public static String sendCODESMS(String mobiles,String msgtest)
	{
		String responseCode = null;
		Map<String, String> params = new HashMap<String, String>();  
		params.put("action", "send");  
		params.put("userid", "3572");  
		params.put("account", "szyzkj2");  
		params.put("password", "szyzkj456789");  
		params.put("mobile", mobiles);  
		params.put("content",YUNZO+msgtest);  
		params.put("sendTime", "");  
		params.put("extno", "");  
		responseCode = HttpClientUtil.post(SMSAPI,params);
		
		return responseCode;
	}

    /**
     * 检测手机号有效性*
     * @param mobile 手机号码
     * @return 是否有效
     */
    public static final boolean isMobileNo(String mobile){
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,1-9]))\\d{8}$");
        Matcher m = p.matcher(mobile);
        return m.matches();
    }
	/**
	*
	*最新 发送短信验证码*
	**/
   public static HashMap<String, Object> sendSMSCode(String mid,String code)
	{
		HashMap<String, Object> result = null;
		//初始化SDK
		CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
		restAPI.init("app.cloopen.com", "8883");
		restAPI.setAccount("8a48b5514eaf512c014eb47e4370071f", "ccf33bfb4416425c876ca00c533933b4");
		restAPI.setAppId("8a48b5514eaf512c014eb480707a0728");
		result = restAPI.sendTemplateSMS(mid,"52133" ,new String[]{code,"30"});		
		return result;
	}

    /**
     * 生成短信验证码*
     * @param length 长度
     * @return 指定长度的随机短信验证码
     */
    public static final String randomSMSCode(int length) {
        boolean numberFlag = true;
        String retStr = "";
        String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
        int len = strTable.length();
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            for (int i = 0; i < length; i++) {
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr += strTable.charAt(intR);
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);
        return retStr;
    }

    /**
     * 发送短信验证码*
     * @param mobile 手机号码
     * @param code 验证码
     * @return 是否发送成功
     */
    public static final boolean sendCode(String mobile, String code) {
        
        //todo 这里实现短信发送功能
    	HashMap<String, Object> result =sendSMSCode(mobile,code);
    	System.out.println("======>>"+result);
        return true;
    }
    
   
}
